new Vue({
    el: '#app',
    data() {
        return {
            Purchase_Order_SerialNumber: "",
            Wares: [],
            rules: {
                Store_SerialNumber: [{ required: true, message: '请选择仓库', trigger: 'change' }],
                Receipt_Type: [{ required: true, message: '请选择存货地点', trigger: 'change' }],
            },
            options: [
                {
                    label: '仓库',
                    value: 0
                },
                // {
                //   label: '店铺',
                //   value: 1,
                // }
            ],
            formData:[],
            formware: {
                Purchase_Receipt_SerialNumber: "",
                Receipt_Date: "",
                Receipt_Type: 0,
                Store_SerialNumber: "",
                Children: {
                    purchase_receipt_detail: [{
                        Purchase_Receipt_Detail_SerialNumber: "",
                        Purchase_Receipt_SerialNumber: "",
                        Purchase_Order_SerialNumber: "",
                        Article_SerialNumber: "",//商品建
                        Receipt_Quantity: "",//入库数量
                        Remain_Quantity:"",//剩余数量
                        Out_Total_Quantity:"",//出库数量
                        Amount: "",//价格
                        Expiration_Date:"",//过期时间
                    }]
                }
            },
            ArticleAndBrand:[],
            loading: false,
        }
    },
    mounted() {
        this.Purchase_Order_SerialNumber = window.location.search.slice(29)
        this.getWares()
        this.GetArticleAndBrand(this.Purchase_Order_SerialNumber)
    },
    computed:{
        ArticleName(){
            return function (id){
                let str = ''
                if (id) {
                    if (this.ArticleAndBrand.some(item=>item.Purchase_Order_Detail_SerialNumber==id)) {
                        let obj = this.ArticleAndBrand.filter(item=>item.Purchase_Order_Detail_SerialNumber==id)[0]
                        return obj.Article_Name
                    }
                    return str
                }else{
                    return str
                }
            }
        },},
    methods: {
        getWares() {//获取所有可选仓库
            let this_ = this
            $.ajax({
                url: DomainName + '/getInquiryAction?token=' + $.cookie("token"),
                type: 'POST',
                data: {
                    Data: 'warehouse_info',
                },
                success: function (res) {
                    res.forEach(item => {
                        this_.Wares.push({
                            label: item.Warehouse_Name,
                            value: item.Applicable_Type == '0' ? item.Warehouse_SerialNumber : item.Parent_Warehouse_SerialNumber
                        })
                    })
                    this_.getGoods(this_.Purchase_Order_SerialNumber)
                    console.log(this_.Wares)
                },
                dataType: "json"
            });
        },
        getGoods(Purchase_Order_SerialNumber) {//获取所选商品
            let this_ = this
            $.ajax({
                url: DomainName + '/getInquiryIDOriginal?token=' + $.cookie("token"),
                type: 'POST',
                data: {
                    Primary_Key_Name: 'Purchase_Order_SerialNumber',
                    Primary_Key: Purchase_Order_SerialNumber,
                    Data: 'purchase_order_detail',
                    isNullEnterprise: true
                },
                success: function (res) {
                    console.log(res)
                    this_.formAcceptance = res
                    this_.formware.Children.purchase_receipt_detail = []
                    this_.formware.Store_SerialNumber = res[0].Store_SerialNumber
                    this_.formAcceptance.forEach(item => {
                        this_.formware.Children.purchase_receipt_detail.push({
                            Purchase_Order_Detail_SerialNumber:item.Purchase_Order_Detail_SerialNumber,
                            Purchase_Receipt_Detail_SerialNumber: "",
                            Purchase_Receipt_SerialNumber: "",
                            Purchase_Order_SerialNumber: item.Purchase_Order_SerialNumber,
                            Article_SerialNumber: item.Article_SerialNumber,
                            Receipt_Quantity: item.Receipt_Quantity,
                            Amount: item.Article_UnitPrice
                        })
                    })
                },
                dataType: "json"
            });
        },
        Sumbit(formName) {// 入库
            let this_ = this
            this_.loading = true
            this.formware.Receipt_Date = getYMDHMSDate()
            this.formware.Enterprise_Info_SerialNumber = $.cookie("enterprise")
            this.formware.Children.purchase_receipt_detail[0].Remain_Quantity = this.formware.Children.purchase_receipt_detail[0].Receipt_Quantity
            this.formware.Children.purchase_receipt_detail[0].Out_Total_Quantity = 0
            console.log(this.formware)
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    $.ajax({
                        url: DomainName + '/setInventoryAction?token=' + $.cookie("token"),
                        type: 'POST',
                        data: {
                            Data: JSON.stringify({ 'purchase_receipt': [this_.formware] })
                        },
                        success: function (res) {
                            console.log(res)
                            if (res.Status == 'success') {
                                this_.GetInquiry(this_.Purchase_Order_SerialNumber, 7)
                            }
                            this_.loading = false
                        },
                        dataType: "json"
                    });
                } else {
                    this_.loading = false
                    this_.$message({
                        message: '请检查表单',
                        type: 'warning'
                    });;
                    return false;
                }
            });
        },
        GetInquiry(Primary_Key, i) {//查原值
            let this_ = this
            $.ajax({
                url: DomainName + '/getInquiryIDOriginal?token=' + $.cookie("token"),
                type: 'POST',
                data: {
                    Primary_Key_Name: 'Purchase_Order_SerialNumber',
                    Primary_Key,
                    Data: 'purchase_order',
                },
                success: function (res) {
                    console.log(res)
                    this_.formData = res[0]
                    this_.formData.Row_Status = i
                    if (this_.formData.Row_Status = i) {
                        this_.AddInquiry(this_.formData)
                    }
                },
                dataType: "json"
            });
        },
        AddInquiry(formData) {//添加
            let this_ = this
            let data = {
                Data: JSON.stringify(
                    {
                        Modify: { 'purchase_order': [formData] },
                    }
                )
            }
            console.log(formData)
            $.ajax({
                url: DomainName + '/getEditAction?token=' + $.cookie("token"),
                type: 'POST',
                data,
                success: function (res) {
                    console.log(res)
                    if(res.Status = 'success'){
                        this_.$message({
                            message: '已入库',
                            type: 'success'
                        });
                        window.history.go(-1)
                    }
                },
                dataType: "json"
            });
        },
        back() {
            window.history.go(-1)
        },
        GetArticleAndBrand(Purchase_Order_SerialNumber){
            const this_ = this
            $.ajax({
                url: DomainName + '/getPurchaseOrderDetailIDAction?token=' + $.cookie("token"),
                type: 'POST',
                data:{
                    Purchase_Order_SerialNumber,
                },
                success: function (res) {
                    console.log(res)
                    this_.ArticleAndBrand = res.Data
                },
                complete: function (XMLHttpRequest) {
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                },
                dataType: "json"
            });
        },
        Pricethousands(num){//转千分位
            return thousands(num)
        },
    },
})